package com.atwisdom.star.thirdInterface.pojo;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Set;

/**
 * 
 * @TableName inf_instance
 */
@TableName(value ="inf_instance" ,autoResultMap = true)
@Data
public class InfInstance implements Serializable {
    /**
     * 主键标识
     */
    @TableId(value = "id")
    private String id;


    /**
     * 接口系统名称
     */
    @TableField(value = "infSysName")
    private String infSysName;

    /**
     * 第三方系统接口数据
     */
    @TableField(value = "infIns",typeHandler = FastjsonTypeHandler.class)
    private JSONObject infIns;

    /**
     * star系统接口数据
     */
    @TableField(value = "entityIns",typeHandler = FastjsonTypeHandler.class)
    private JSONObject entityIns;

    /**
     * 协议
     */
    @TableField(value = "protocol",typeHandler = FastjsonTypeHandler.class)
    private JSONObject protocol;

    /**
     * 说明
     */
    @TableField(value = "comments")
    private String comments;

    /**
     * 创建人账号
     */
    @TableField(value = "createdBy",fill = FieldFill.INSERT)
    protected String createdBy;

    /**
     * 创建时间
     */
    @TableField(value = "createTime",fill = FieldFill.INSERT)
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss")
    protected LocalDateTime createTime;

    /**
     * 修改人账号
     */
    @TableField(value = "updatedBy",fill = FieldFill.INSERT_UPDATE)
    protected String updatedBy;

    /**
     * 修改时间
     */
    @TableField(value = "updateTime",fill = FieldFill.INSERT_UPDATE)
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss")
    protected LocalDateTime updateTime;

    @TableField(exist = false)
    private static final long serialVersionUID = 1L;

    public InfInstance(){

    }

    /**
     * 有参构造方法
     *
     * @param jsonObject 参数
     */
    public InfInstance(JSONObject jsonObject) {
        Set<String> keys = jsonObject.keySet();
        this.id = keys.contains("id") ? jsonObject.getString("id") : null;
        this.infSysName = keys.contains("infSysName") ? jsonObject.getString("infSysName") : null;
        this.infIns = keys.contains("infIns") ? jsonObject.getJSONObject("infIns") : null;
        this.entityIns = keys.contains("entityIns") ? jsonObject.getJSONObject("entityIns") : null;
        this.protocol = keys.contains("protocol") ? jsonObject.getJSONObject("protocol") : null;
        this.comments = keys.contains("comments") ? jsonObject.getString("comments") : null;
    }

    /**
     * 将当前模型对象转成jsonObject
     *
     * @return 返回结果
     */
    public JSONObject fetchJsonObject() {
        JSONObject JsonObject = new JSONObject();
        JsonObject.put("id", this.id);
        JsonObject.put("infSysName", this.infSysName);
        JsonObject.put("infIns", this.infIns);
        JsonObject.put("entityIns", this.entityIns);
        JsonObject.put("protocol", this.protocol);
        JsonObject.put("comments", this.comments);
        JsonObject.put("createdBy", this.createdBy);
        JsonObject.put("createTime", this.createTime);
        JsonObject.put("updatedBy", this.updatedBy);
        JsonObject.put("updateTime", this.updateTime);
        return JsonObject;
    }
}